import { getCuesTopicDetails, selectCuesTopicManage, selectCuesTopicManageTimeline, updateTopicManageRecordState } from "../../../../service/api/api-cues-topic";
import Toast from 'tdesign-miniprogram/toast/index';
// pages/cues/topic/details/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentUser: {},//当前用户
    access: {} as any,// 当前用户权限
    loading: false,//是否加载中
    topicId: '',//该选题的id
    recordId: "",//该选题的记录id
    mode: 0,//该征集的类型
    topicDetails: {},//该选题的详情
    activeAssignOrganizations: false,//指派征集单位是否显示
    activeLinkageOrganizations: false,//协助媒体单位是否显示
    showDescribes: false,//操作描述框是否显示
    handleName: '',//操作名称
    handleContent: '',//操作提示描述
    handelDescribes: '',//操作描述
    buttonRecordState: 0,//操作按钮状态
    grade: 1,//分类
    timeLineList: [],//时间轴数据
    showMyTopic: false,//是否显示选题操作提示(我的选题征集)
    buttonPublishGrade: 1,//发布弹窗选择的等级
  },
  //操作详情
  async toDetailPage(e) {
    wx.showLoading({
      title: "加载中"
    })
    const response = await selectCuesTopicManage({
      id: e.currentTarget.dataset.id,
      recordId: e.currentTarget.dataset.recordid,
    });
    const { code, data } = response;
    if (code === 0) {
      this.setData({
        topicDetails: data
      })
      wx.hideLoading()
    }
  },
  //申请操作（我的选题征集）
  handleSaveMyTopic() {
    this.openDescribes()
    this.handleSave()
    // this.closeMyTopic()
  },
  //废弃操作（我的选题征集）
  handlWasteMyTopic(e) {
    this.setData({
      handleContent: e.currentTarget.dataset.content,
      handleName: e.currentTarget.dataset.name,
      buttonRecordState: 4,
      handelDescribes: '',
    })
    this.openDescribes()
    // this.openMyTopic()
  },
  //撤回操作（我的选题征集）
  handleBackMyTopic(e) {
    this.setData({
      handleContent: e.currentTarget.dataset.content,
      handleName: e.currentTarget.dataset.name,
      buttonRecordState: 5,
      handelDescribes: '',
    })
    // this.openMyTopic()
    this.openDescribes()
  },
  //确认操作（我的选题征集）
  async confirmMyTopic() {
    this.confirmDescribes()
    this.closeMyTopic()
    wx.reLaunch({
      url: '/pages/cues/topic/index?mode=' + this.data.mode
    })// 回到上一个界面
  },
  //打开提示框操作（我的选题征集）
  openMyTopic() {
    this.setData({
      showMyTopic: true
    })
  },
  //关闭提示框操作（我的选题征集）
  closeMyTopic() {
    this.setData({
      showMyTopic: false
    })
  },
  //改变分类
  handleChangeGrade(e) {
    this.setData({
      grade: e.detail.value,
      buttonPublishGrade: e.detail.value
    })
  },
  //打开操作描述框
  openDescribes() {
    this.setData({
      showDescribes: true
    })
  },
  //关闭操作描述框
  closeDescribes() {
    this.setData({
      showDescribes: false
    })

  },
  //操作描述框确认
  async confirmDescribes() {

    if (this.data.topicDetails && this.data.topicDetails.recordState == 0) {
      return;
    }

    if (this.data.buttonRecordState !== 3 && (this.data.handelDescribes === '' || this.data.handelDescribes === undefined || this.data.handelDescribes === null)) {
      wx.showToast({
        title: '请输入描述'
      })
      return;
    }
    Toast({
      context: this,
      selector: '#t-toast',
      message: '提交中',
      direction: 'column',
      duration: 3000,
      preventScrollThrough: true,
      theme: 'loading',
    });
    const response = await updateTopicManageRecordState({
      id: this.data.topicId,
      recordState: this.data.buttonRecordState,
      updateSource: 1,
      grade: this.data.grade,
      recordDescribes: this.data.handelDescribes,
    });
    if (response.code === 0) {
      wx.showToast({
        title: '提交成功',
        icon: "none"
      })
      this.closeDescribes()
      setTimeout(() => {
        wx.reLaunch({
          url: '/pages/cues/topic/index?mode=' + this.data.mode
        })// 回到上一个界面
      }, 600);
      return true;
    } else {
      this.closeDescribes()
      return false;
    }

  },
  //废弃操作
  handlWaste(e) {
    this.setData({
      handleContent: e.currentTarget.dataset.content,
      handleName: e.currentTarget.dataset.name,
      handelDescribes: '',
      buttonRecordState: 4,
    })
    this.openDescribes()
  },
  //退回操作
  handleBack(e) {
    this.setData({
      handleContent: e.currentTarget.dataset.content,
      handleName: e.currentTarget.dataset.name,
      handelDescribes: '',
      buttonRecordState: 2,
    })
    this.openDescribes()
  },
  //发布操作
  handelPublish(e) {
    this.setData({
      handleContent: e.currentTarget.dataset.content,
      handleName: e.currentTarget.dataset.name,
      handelDescribes: '',
      buttonRecordState: 3,
      handelTopicId: e.currentTarget.dataset.topicId,
      buttonPublishGrade: e.currentTarget.dataset.grade
    })
    this.openDescribes()
  },
  //输入操作描述
  onDescribInput(e) {
    this.setData({
      handelDescribes: e.detail.value
    })
  },
  //开关指派征集单位
  handleAssignOrganizationsChange() {
    this.setData({
      activeAssignOrganizations: true,
    });
  },
  //开关协助媒体单位
  handleLinkageOrganizationsChange() {
    this.setData({
      activeLinkageOrganizations: true,
    });
  },
  //获取该选题的详情
  async getTopicDetails(id) {
    this.setData({
      loading: true
    })

    try {
      const response = await selectCuesTopicManage({ id });
      if (response.code === 0) {
        this.setData({
          topicDetails: response.data,
          buttonPublishGrade: response.data.grade
        })
        this.setData({
          loading: false
        })
      }
    } catch (error) { }
  },
  //修改该选题的详情
  handleSave() {
    wx.navigateTo({
      url: '/pages/cues/topic/create/index?topicId=' + this.data.topicId + '&mode=' + this.data.mode
    })
  },

  /**
   * 加载 时间轴
   * @param key
   */
  async loadSelectCuesTopicManageTimelineData() {
    const response = await selectCuesTopicManageTimeline({ id: this.data.topicId });
    const { code, data } = response;
    if (code === 0) {
      this.setData({
        timeLineList: data
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: { topicId: any; mode: any, recordId: string }) {
    const { globalData: { access, currentUser } } = getApp();
    this.getTopicDetails(options.topicId)
    this.setData({
      access,
      currentUser,
      topicId: options.topicId,
      mode: options.mode,
      recordId: options.recordId
    })
    this.loadSelectCuesTopicManageTimelineData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //返回页面刷新
    this.getTopicDetails(this.data.topicId)
    this.loadSelectCuesTopicManageTimelineData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})